<?php
namespace Home\Model;

use Think\Model;
class StaffModel extends Model{
    //自动验证
    protected $_validate = array(
        //员工姓名2-20位
        array('name','2,20','员工姓名长度不合法',self::EXISTS_VALIDATE,'length',self::MODEL_INSERT),

        //员工工号被占用 -1
        array('number','',-1,self::EXISTS_VALIDATE,'unique',self::MODEL_BOTH),
        //身份证号重复   -2
        array('id_card','',-2,self::EXISTS_VALIDATE,'unique',self::MODEL_BOTH),


        //帐号密码6-30位  后台 帐号新增
        array('password','6,30','帐号密码长度不合法',self::EXISTS_VALIDATE,'length',self::MODEL_INSERT),

        //帐号密码6-30位  后台 帐号修改
        array('password','6,30','帐号密码长度不合法',self::VALUE_VALIDATE,'length',self::MODEL_UPDATE),

        //确认密码和密码不一致 login 前台
        //确认密码和密码不一致 后台  修改密码
        array('password','notpassword','确认密码和密码不一致',self::EXISTS_VALIDATE,'confirm',self::MODEL_BOTH),


        // login  登录帐号验证
        array('accounts','2,20','登录帐号长度不合法',self::EXISTS_VALIDATE,'length',4),
        // login  登录密码验证
        array('password','6,30','登录密码长度不合法',self::EXISTS_VALIDATE,'length',4),


    );

    public function getNotRelationList($sort,$order,$page,$rows) {
        //得到所有的节点
        $map['user_id'] = 0;
        $object = $this -> field('id,name,number,gender,id_card,post,create_time')
            -> where($map)
            -> order(array($sort=>$order))
            -> limit($rows * ($page-1) , $rows)
            -> select();
        return array(
            'total' => $this->count(),
            'rows' => $object ? $object : ''
        );
    }

    public function getList($sort,$order,$page,$rows,$keywords,
                    $dateType,$dateFrom,$dateTo,$id_card,$nation,
                    $gender,$marital_status,$type,$post,$entry_status,$education) {

        $map = array();

        //关键字
        if ($keywords) {
            $keywords_map['name'] = array('like','%'.$keywords.'%');
            $keywords_map['tel'] = array('like','%'.$keywords.'%');
            $keywords_map['number'] = array('like','%'.$keywords.'%');
            $keywords_map['_logic'] = 'OR';
            $map['_complex'] = $keywords_map;
        }

        //身份证
        if ($id_card) {
            $map['id_card'] = $id_card;
        }
        //民族
        if ($nation) {
            $map['nation'] = $nation;
        }
        //员工类型
        if ($type) {
            $map['type'] = $type;
        }
        //婚姻状态
        if ($marital_status) {
            $map['marital_status'] = $marital_status;
        }
        //性别
        if ($gender) {
            $map['gender'] = $gender;
        }
        //职位
        if ($post) {
            $map['post'] = $post;
        }
        //在职状况
        if ($entry_status) {
            $map['entry_status'] = $entry_status;
        }
        //学历
        if ($education) {
            $map['education'] = $education;
        }

        if ($dateFrom && $dateTo) {
            $map["$dateType"] = array(
                array('egt', $dateFrom),
                array('elt', $dateTo),
                );

        } else if ($dateFrom) {
            $map["$dateType"] = array('egt', $dateFrom);
        } else if ($dateTo) {
            $map["$dateType"] = array('elt', $dateTo);
        }

        //得到所有的节点
        $object = $this -> field('id,user_id,name,number,gender,id_card,
                                  marital_status,nation,type,post,
                                  tel,entry_status,entry_date,dimission_date,
                                  politics_status,education,create_time'

        )
                        -> where($map)
                        -> order(array($sort=>$order))
                        -> limit($rows * ($page-1) , $rows)
                        -> select();
        return array(
            'total' => $this->count(),
            'rows' => $object ? $object : ''
        );

    }
    //前后台  新增帐号操作[not=1 是login 前台新增帐号操作]
    public function register($name,$number,$tel,$post,
                             $id_card,$intro,$registered_address,$registered,
                             $politics_status,$details,$nation,$dimission_date,
                             $education,$gender,$health,$graduate_date,
                             $graduate_colleges,$entry_date,$entry_status,$marital_status,
                             $type,$specialty) {
        $addData = array(
            'name'              => $name,
            'number'            => $number,
            'post'              => $post,
            'tel'               => $tel,
            'type'              => $type,
            'gender'            => $gender,
            'marital_status'    => $marital_status,
            'nation'            => $nation,
            'id_card'           => $id_card,
            'entry_date'        => $entry_date ? $entry_date : '0000-00-00',
            'dimission_date'    => $dimission_date ? $dimission_date : '0000-00-00',
            'entry_status'      => $entry_status,
            'politics_status'   => $politics_status,
            'education'         => $education,
            'create_time'       => getTime()
        );


        if ($this->create($addData)) {
            $id = $this->add($addData);
            if ($id) {
                M('StaffExtend')->add(array(
                    'staff_id'              => $id,
                    'intro'                 => $intro,
                    'registered_address'    => $registered_address,
                    'registered'            => $registered,
                    'details'               => $details,
                    'health'                => $health,
                    'graduate_date'         => $graduate_date,
                    'graduate_colleges'     => $graduate_colleges,
                    'specialty'             => $specialty,
                ));
                return $id;
            } else {
                return 0;
            }


        } else{
            if ($this->getError() == -1) {
                return -1;
            }
            if ($this->getError() == -2) {
                return -2;
            }
            return $this->getError();
        }
    }
    //根据ID 获取一条编辑的数据
    public function getOne($id) {
        $map['id'] = $id;

        $object = $this->field('crm_staff.id,
                             crm_staff.name,
                             crm_staff.number,
                             crm_staff.tel,
                             crm_staff.post,
                             crm_staff.id_card,
                             crm_staff.politics_status,
                             crm_staff.nation,
                             crm_staff.dimission_date,
                             crm_staff.education,
                             crm_staff.gender,
                             crm_staff.type,
                             crm_staff.entry_date,
                             crm_staff.entry_status,
                             crm_staff.marital_status,
                             
                             crm_staff_extend.health,
                             crm_staff_extend.details,
                             crm_staff_extend.intro,
                             crm_staff_extend.registered,
                             crm_staff_extend.registered_address,
                             crm_staff_extend.graduate_date,
                             crm_staff_extend.graduate_colleges,
                             crm_staff_extend.specialty')
                    ->join('crm_staff_extend ON crm_staff.id = crm_staff_extend.staff_id', 'LEFT')
                    ->where($map)
                    ->find();

        $object['details'] = htmlspecialchars_decode($object['details']); //实体转义 如 &gt;
        return $object;
    }


    //根据ID 修改一条数据 [返回影响的行数]
    public function update($id,$name,$number,$tel,$post,
                           $id_card,$intro,$registered_address,$registered,
                           $politics_status,$details,$nation,$dimission_date,
                           $education,$gender,$health,$graduate_date,
                           $graduate_colleges,$entry_date,$entry_status,$marital_status,
                           $type,$specialty) {

        $updateData = array(
            'id'                => $id,
            'name'              => $name,
            'number'            => $number,
            'post'              => $post,
            'tel'               => $tel,
            'type'              => $type,
            'gender'            => $gender,
            'marital_status'    => $marital_status,
            'nation'            => $nation,
            'id_card'           => $id_card,
            'entry_date'        => $entry_date ? $entry_date : '0000-00-00',
            'dimission_date'    => $dimission_date ? $dimission_date : '0000-00-00',
            'entry_status'      => $entry_status,
            'politics_status'   => $politics_status,
            'education'         => $education,
        );

        if($this->create($updateData)) {
            //修改主表
            $affectRow = $this->save($updateData);

            //修改附表
            $updateExtendData = array(
                'staff_id'              => $id,
                'intro'                 => $intro,
                'registered_address'    => $registered_address,
                'registered'            => $registered,
                'details'               => $details,
                'health'                => $health,
                'graduate_date'         => $graduate_date,
                'graduate_colleges'     => $graduate_colleges,
                'specialty'             => $specialty,
            );

            $map['staff_id'] = $id;
            $extendAffectRow = M('StaffExtend')->where($map)->save($updateExtendData);

            return $affectRow || $extendAffectRow;

        } else {
            if ($this->getError() == -1) {
                return -1;
            }
            if ($this->getError() == -2) {
                return -2;
            }
            return $this->getError();
        }

    }


    //根据ID 删除一条或者多条数据 [返回影响的行数]
    public function remove($ids) {
        $afectRow =  $this->delete($ids);
        if ($afectRow) {
            //删除附表
            $map['staff_id'] = array('in',$ids);
            M('StaffExtend')->where($map)->delete();
        }

        return $afectRow;
    }


}
